package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.TianJieWalletBlockRecord;
import org.itboys.mobile.entity.mysql.TianJieWalletProduct;
import org.itboys.mobile.entity.mysql.TianJieWalletRecord;
import org.itboys.mobile.service.mysql.web.TianJieWalletProductService;
import org.itboys.mobile.service.mysql.web.TianJieWalletService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_金融_消费卡_充值产品_controller_web
 */
@Controller
@RequestMapping("/tianjie/web/finance/wallet")
public class TianJieWalletController extends BaseController{
    @Autowired
    private TianJieWalletProductService walletProductService;
    @Autowired
    private TianJieWalletService walletService;

    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/product/index")
    public String index(HttpServletRequest request,
                        Model model){
            return "/tianjie/wallet/product/index";
    }


    /**
     * 分页查询商品
     * @param request
     * @param response
     */
    @RequestMapping("/product/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response){
        try{
            PageResult<TianJieWalletProduct> result=walletProductService.selectProduct(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 充值产品
     * @param request
     * @param response
     * @param product
     */
    @RequestMapping("/product/save")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute TianJieWalletProduct product){
        try {
            walletProductService.saveProduct(product);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/wallet/product/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }

    /**
     * 单个查询
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/product/findOne")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="id",required = true)Long id){
        try {
            TianJieWalletProduct product=walletProductService.findOne(id);
            ResponseMessage.success(product);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/wallet/product/findOne------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);

        }
    }
    /**
     * 删除
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/product/delete")
    public void delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="id",required = true)Long id){
        try {
            walletProductService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/wallet/product/delete------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    /**------------------消费卡记录
    /**
     * 提现-主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/account/index")
    public String record_cash_index(HttpServletRequest request,
                        Model model){
        return "/tianjie/wallet/account/index";
    }


    /**
     * 查询提现列表
     * @param request
     * @param response
     */
    @RequestMapping("/record/account/select")
    public void record_account_select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="accountStatuses",required = true)List<Integer> accountStatuses
                       ){
        try{
            PageResult<TianJieWalletRecord> result=walletService.selectAccountRecord(request,accountStatuses);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 查询提现列表
     * @param request
     * @param response
     */
    @RequestMapping("/record/member/select")
    public void record_member_select(HttpServletRequest request,
                                      HttpServletResponse response,
                                      @RequestParam(value="memberId",required = true)Long memberId
    ){
        try{
            PageResult<TianJieWalletRecord> result=walletService.selectMemberRecord(request,memberId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 处理提现申请
     * @param request
     * @param response
     * @param recordId
     * @param isAgree
     */
    @RequestMapping("/record/handle")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="recordId",required = true)Long recordId,
            @RequestParam(value="isAgree",required = true)boolean isAgree,
            @RequestParam(value="remark",required = false)String remark
    ){
        try {
            walletService.record_handle(recordId,isAgree,remark);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/wallet/record/handle------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }


}
